package cn.eeepay.framework.service.workOrder.impl;

import cn.eeepay.framework.dao.workOrder.WorkReadDao;
import cn.eeepay.framework.dao.workOrder.WorkWarningReadDao;
import cn.eeepay.framework.dao.workOrder.WorkWarningWriteDao;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.AgentInfo;
import cn.eeepay.framework.model.workOrder.WaringConfigInfo;
import cn.eeepay.framework.model.workOrder.WorkWarningInfo;
import cn.eeepay.framework.service.AgentInfoService;
import cn.eeepay.framework.service.workOrder.WorkWarningService;
import cn.eeepay.framework.util.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WorkWarningServiceImpl implements WorkWarningService {

    @Resource
    private WorkWarningReadDao workWarningReadDao;
    @Resource
    private WorkWarningWriteDao workWarningWriteDao;
    @Resource
    private WorkReadDao workReadDao;

    @Resource
    private AgentInfoService agentInfoService;

    /**
     * 首页获取工单预警相关信息
     * @param result
     * @param loginAgent 当前登入代理商
     */
    @Override
    public void workWarning(Map<String, Object> result, AgentInfo loginAgent) {

        //获取预警开关
        WaringConfigInfo switchWork=workWarningReadDao.getWaringConfig();
        if(switchWork!=null&& 1==switchWork.getWarningSwitch().intValue()){
            //本月第一天00：00：00.000
            Date startDate= DateUtils.getTimesMonthmorning();
            Date endDate=new Date();
            WorkWarningInfo filterInfo=new WorkWarningInfo();
            filterInfo.setCreateTimeBegin(startDate);
            filterInfo.setCreateTimeEnd(endDate);
            filterInfo.setAgentNo(loginAgent.getAgentNo());
            filterInfo.setWorkProductType("1");
            int count=workWarningReadDao.countWaringMonth(filterInfo);
            result.put("workWarningCount",count);
            if(count>0){
                WorkWarningInfo info=workWarningReadDao.getWaringMonthNow(filterInfo);
                result.put("workWarningInfo",info);
            }
        }
        if(1==loginAgent.getAgentLevel().intValue()){
            int wrokCount=workReadDao.agentCountWork(loginAgent.getAgentNo());
            int wrokCountUntreated=workReadDao.agentCountWorkUntreated(loginAgent.getAgentNo());
            result.put("wrokCount",wrokCount);
            result.put("wrokCountUntreated",wrokCountUntreated);
        }
    }

    /**
     * 工单预警列表查询
     * @param info
     * @param page
     * @return
     */
    @Override
    public List<WorkWarningInfo> selectWorkWarningList(WorkWarningInfo info, Page<WorkWarningInfo> page) {
        List<WorkWarningInfo> list=workWarningReadDao.selectWorkWarningList(info,page);
        return list;
    }

    /**
     * 工单预警 已读
     * @param id
     * @param msg
     * @return
     */
    @Override
    public int closeWorkWarning(Long id, Map<String, Object> msg) {
        //AgentInfo loginAgent = agentInfoService.selectByPrincipal();//登入代理商
        AgentInfo loginAgent = null;
        WorkWarningInfo oldInfo= workWarningWriteDao.checkWorkWarning(id,loginAgent.getAgentNo());
        if(oldInfo==null){
            msg.put("status", false);
            msg.put("msg", "无数据操作权限!");
            return 0;
        }
        int num =workWarningWriteDao.closeWorkWarning(id);
        if(num>0){
            msg.put("status", true);
            msg.put("msg", "操作成功!");
        }else{
            msg.put("status", false);
            msg.put("msg", "操作失败!");
        }
        return num;
    }
}
